//
//  PublicityManageViewController.m
//  iStore
//
//  Created by Geek on 15/4/26.
//  Copyright (c) 2015年 mirrtalk. All rights reserved.
//

#import "PublicityManageViewController.h"
#import "PublicityManageTableViewCell.h"
#import "ADViewController.h"
#import "PutAndFinishADModel.h"
#import "AFHTTPRequestOperationManager.h"
#import "RequestEngine.h"
#import "RepealViewController.h"


#define KSelectedColor [UIColor colorWithRed:109/255.0f green:253/255.0f blue:156/255.0f alpha:1.0f];

#define KUnSelectedColor [UIColor colorWithRed:106/255.0f green:203/255.0f blue:156/255.0f alpha:1.0f]

#define KBackgroundColor [UIColor colorWithRed:91.0/255 green:195.0/255 blue:136.0/255 alpha:1.0]

@interface PublicityManageViewController () <UITableViewDataSource,UITableViewDelegate>
{
    
    BOOL _isHighlighted;
    
    BOOL isdelete;
    
    UIButton * _button;
    
    NSString *_userID;
    UIView *view;
    UILabel *nullLabel;
    
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation PublicityManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"宣传管理";
    self.navigationItem.leftBarButtonItem.title = @"返回";
    
    _userID = [[NSString alloc]init];
    _userID = [[NSUserDefaults standardUserDefaults]objectForKey:kUserId];

    _dataArray = [[NSMutableArray alloc]init];
    _tableView.allowsSelection = NO;
    _tableView.tableFooterView = [[UIView alloc]init];
    
    UIButton * firstBtn = (UIButton *)[self.view viewWithTag:101];
    firstBtn.backgroundColor = KSelectedColor;
    UIButton * subBtn = (UIButton *)[self.view viewWithTag:102];
    subBtn.backgroundColor = KUnSelectedColor;
    UIButton * ThirdBtn = (UIButton *)[self.view viewWithTag:103];
    ThirdBtn.backgroundColor = KUnSelectedColor;
    UIButton * FourthBtn = (UIButton *)[self.view viewWithTag:104];
    FourthBtn.backgroundColor = KUnSelectedColor;


}

- (void)viewWillAppear:(BOOL)animated
{
    [self createrReleaseRequesrtEngine];
}
- (void)createrReleaseRequesrtEngine
{
    
    NSDictionary *dict = @{@"userID":_userID};
    [self activityStart];
    [_dataArray removeAllObjects];
    [RequestEngine releaseADWithDic:dict completed:^(NSString *errorcode, NSDictionary *resultDict) {
        [self activityStop];
        
        if ([errorcode isEqualToString:@"0"]) {
            for (id obj in resultDict) {
                [_dataArray addObject:obj];
            }
            [_tableView reloadData];
            if (_dataArray.count == 0 || _dataArray == nil) {
                [self createNull:@"您还没有添加订单~"];
            }else{
            
                [self createNull:@""];
            }
            
        }
    }];
}

- (void)createFinishRequertEngine
{
    [self activityStart];
    [_dataArray removeAllObjects];
    NSDictionary *dict = @{@"userID":_userID};
    [RequestEngine finishADWithDic:dict completed:^(NSString *errorcode, NSDictionary *resultDict) {
        [self activityStop];
        if ([errorcode isEqualToString:@"0"]) {
            for (id obj in resultDict) {
                [_dataArray addObject:obj];
            }
           [_tableView reloadData];
            if (_dataArray.count == 0 || _dataArray == nil) {
                [self createNull:@"您还没有已完成订单~"];
            }else{
//                [nullLabel removeFromSuperview];
                [self createNull:@""];

            }
            
        }
        
    }];
}

- (void)creatertakeBackRequesrtEngine
{
    NSDictionary *dict = @{@"userID":_userID};
    [self activityStart];
    [_dataArray removeAllObjects];
    [RequestEngine takeBackADWithDic:dict completed:^(NSString *errorcode, NSDictionary *resultDict) {
        [self activityStop];
        if ([errorcode isEqualToString:@"0"]) {
            for (id obj in resultDict) {
                [_dataArray addObject:obj];
            }
            [_tableView reloadData];
            if (_dataArray.count == 0 || _dataArray == nil) {
                [self createNull:@"您还没有已回收订单~"];
            }else{
                [self createNull:@""];
            }
            
        }
    }];
}
- (void)createUnFinishRequertEngine
{
    [self activityStart];
    [_dataArray removeAllObjects];
    NSDictionary *dict = @{@"userID":_userID};
    [RequestEngine RequestEngineWithDic:dict URL:@"queryNotVerifyAD" completed:^(NSString *errorcode, NSDictionary *resultDict) {
        [self activityStop];
        if ([errorcode isEqualToString:@"0"]) {
            for (id obj in resultDict) {
                [_dataArray addObject:obj];
            }
            [_tableView reloadData];
            if (_dataArray.count == 0 || _dataArray == nil) {
                [self createNull:@"您没有待审核的订单~"];
            }else{
                //                [nullLabel removeFromSuperview];
                [self createNull:@""];
                
            }
        }
        
    }];
}
- (void)createNull:(NSString *)str
{
    if (!nullLabel) {
        nullLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 160, ScreenWidth, 40)];
        nullLabel.textColor = [UIColor blackColor];
        [nullLabel setTextAlignment:NSTextAlignmentCenter];
        [self.view addSubview:nullLabel];
    }
    
    nullLabel.text = str;
    
}

#pragma  mark - tableView --
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    PublicityManageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"PublicityManageTableViewCell" owner:self options:nil]lastObject];
    }
    [cell releaseAdData:[_dataArray objectAtIndex:indexPath.row]];
    
    return cell;
}

#pragma mark --撤销--

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{    if (_button.tag == 102 || _button.tag == 103 || _button.tag == 104) {
        return UITableViewCellEditingStyleNone;
    
    }
    else
        return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    RepealViewController * repeVC = [[RepealViewController alloc]init];
    
    repeVC.orderType = [[_dataArray objectAtIndex:indexPath.row] objectForKey:@"orderFormNumber"];
    
    [self.navigationController pushViewController:repeVC animated:YES];
    
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"撤销";
}


- (IBAction)addBtnClick:(UIButton *)sender
{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"regulation" ofType:@"txt"];
    view = [[UIView alloc]initWithFrame:self.view.bounds];
    view.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.3];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 80, ScreenWidth-40, 40)];
    label.text = @"“声财”语音发布规则";
    [label setTextColor:[UIColor whiteColor]];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setBackgroundColor:KBackgroundColor];
    
    UITextView *contentView = [[UITextView alloc]initWithFrame:CGRectMake(20, 120, ScreenWidth - 40, ScreenHeight - 190)];
    contentView.editable = NO;
    contentView.selectable = NO;
    [contentView setTextColor:[UIColor blackColor]];
    [contentView setText:[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]];
    
    UIButton *agreeButton = [[UIButton alloc]initWithFrame:CGRectMake(60, ScreenHeight - 60, (ScreenWidth - 150)/2, 30)];
    agreeButton.tag = 100;
    [agreeButton setTitle:@"同意" forState:UIControlStateNormal];
    [agreeButton addTarget:self action:@selector(protocolChoose:) forControlEvents:UIControlEventTouchUpInside];
    [agreeButton setBackgroundColor:KBackgroundColor];
    
    UIButton *disagreeButton = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2 + 15, ScreenHeight-60, (ScreenWidth - 150)/2, 30)];
    disagreeButton.tag = 101;
    [disagreeButton setTitle:@"不同意" forState:UIControlStateNormal];
    [disagreeButton addTarget:self action:@selector(protocolChoose:) forControlEvents:UIControlEventTouchUpInside];
    [disagreeButton setBackgroundColor:[UIColor colorWithWhite:0 alpha:.5f]];
    
    UIView *addView = [[UIView alloc]initWithFrame:CGRectMake(20,  ScreenHeight-70, ScreenWidth - 40, 50)];
    addView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:view];
    [view addSubview:addView];
    [view addSubview:label];
    [view addSubview:contentView];
    [view addSubview:agreeButton];
    [view addSubview:disagreeButton];
    
}
- (void)protocolChoose:(UIButton*)btn
{
    [view removeFromSuperview];
    if (btn.tag == 100) {
        ADViewController * adVC = [[ADViewController alloc]init];
        [self.navigationController pushViewController:adVC animated:YES];
    }
    else if (btn.tag == 101) {
        return;
    }
}
- (IBAction)putBtnClick:(UIButton *)sender {
    
    _button = sender;

    if (sender.tag == 101) {
        [self createrReleaseRequesrtEngine];
        sender.backgroundColor = KSelectedColor;
        
        UIButton *subBtn = (UIButton *)[self.view viewWithTag:102];
        UIButton *lastBtn = (UIButton *)[self.view viewWithTag:103];
        UIButton *finalBtn = (UIButton *)[self.view viewWithTag:104];
        
        subBtn.backgroundColor = KUnSelectedColor;
        lastBtn.backgroundColor = KUnSelectedColor;
        finalBtn.backgroundColor = KUnSelectedColor;
        

        
    }else if (sender.tag == 102){
        [self createFinishRequertEngine];
        
        UIButton * subBtn = (UIButton *)[self.view viewWithTag:101];
        UIButton * lastBtn = (UIButton *)[self.view viewWithTag:103];
        UIButton * finalBtn = (UIButton *)[self.view viewWithTag:104];

        subBtn.backgroundColor = KUnSelectedColor;
        lastBtn.backgroundColor = KUnSelectedColor;
        finalBtn.backgroundColor = KUnSelectedColor;
        
        sender.backgroundColor = KSelectedColor;
    }else if (sender.tag == 103){
        
        [self creatertakeBackRequesrtEngine];
        
        UIButton * firstBtn = (UIButton *)[self.view viewWithTag:101];
        UIButton * subBtn = (UIButton *)[self.view viewWithTag:102];
        UIButton * finalBtn = (UIButton *)[self.view viewWithTag:104];

        subBtn.backgroundColor = KUnSelectedColor;
        firstBtn.backgroundColor = KUnSelectedColor;
        finalBtn.backgroundColor = KUnSelectedColor;

        sender.backgroundColor = KSelectedColor;
    }else {
    
        [self createUnFinishRequertEngine];
    
        UIButton * firstBtn = (UIButton *)[self.view viewWithTag:101];
        UIButton * subBtn = (UIButton *)[self.view viewWithTag:102];
        UIButton * finalBtn = (UIButton *)[self.view viewWithTag:103];
        

        subBtn.backgroundColor = KUnSelectedColor;
        firstBtn.backgroundColor = KUnSelectedColor;
        finalBtn.backgroundColor = KUnSelectedColor;

        sender.backgroundColor = KSelectedColor;
    }
}

@end
